<?php

include_once('staff.php');

class loan_management extends staff
{
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		
		// Load the form helper
		$this->load->helper('form');
		
		// Load the form validation library
		$this->load->library('form_validation');
		
		// Load the required models
		$this->load->model('subscriber_model');
		$this->load->model('subscriber_paid_model');
		$this->load->model('loan_model');
		$this->load->model('loan_history_model');
		$this->load->model('volume_model');
    }
	
	public function addLoan($subscriber_id = 0)
	{
		// If form has been submitted...
		if ( $this->input->post() )
		{
			// If form validation succeeds (All fields correct)
			if ($this->form_validation->run('staff/loan_management/add'))
			{
				// Get the data to insert
				$data = $this->input->post();
			
				// If this item is already on loan...
				if ($this->loan_model->getLoanByID($data['access_id'])->row())
					$this->data['error'] = 'ERROR: Item already on loan';
				
				// Add loan and set success message
				else
				{
					$this->loan_model->addLoan( $data );
					$this->data['error'] = 'Item #' . $data['access_id'] . ' successfully loaned.';
				}
			}
		}
	
		// Load view to select subscriber if one has not been selected
		if ( !$subscriber_id )
		{
			// Load the "add_loan" view
			$this->data['main_content'] = $this->load->view('staff/loan_management/add_loan_select_subscriber', $this->data, true);
		
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}
		
		// Otherwise, load view to select volume
		else
		{
			// Return subscriber information
			$this->data['subscriber'] = $this->subscriber_model->getSubscriberByID($subscriber_id)->row();
			
			// Return last payment from subscriber
			$this->data['subscriber_paid'] = $this->subscriber_paid_model->getSubscriberPaidByID($subscriber_id)->row();
		
			// Load the "add_loan" view
			$this->data['main_content'] = $this->load->view('staff/loan_management/add_loan', $this->data, true);
		
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);			
		}
	}
	
	public function returnLoan($access_id = 0)
	{
		// If 'POST' data exists (i.e. the submit button was clicked) or access_id specified in url...
		if ( $this->input->post() || $access_id )
		{
			// If no access_id was specified in URL, get it from post
			if ( !$access_id )
				$access_id = $this->input->post('access_id');
			
			// Add the loan to tbl_loan_history
			$loan = $this->loan_model->getLoanByID( $access_id )->row();
			$loan->return_date = date('Y-m-d');
			$this->loan_history_model->addLoan( $loan );

			// And delete it from tbl_loan
			$this->loan_model->deleteLoanByID( $access_id );

			// If access_id was specified in URL, redirect to previous page
			if ( !$this->input->post() )
				redirect( $_SERVER['HTTP_REFERER'] );

			// Otherwise set message
			else	
				$this->data['error'] = 'Volume successfully returned.';
			
			// If item had a reserve subscriber print their details
			if ( $loan->reserve_subscriber )
			{
				$res_sub = $this->subscriber_model->getSubscriberByID($loan->reserve_subscriber)->row();
				$this->data['error'] .=	'<br/><br/><strong>' 
										. $this->volume_model->getVolumeByID($loan->access_id)->row()->title
										. ' has been reserved by ' . $res_sub->first_name . ' ' . $res_sub->last_name . '.</strong><br/>'
										. 'Please call them on one of the following numbers to notify them of its availability: <br/>'
										. 'Home Number: ' . $res_sub->phone . '<br/>'
										. 'Mobile Number: ' . $res_sub->mobile_phone . '<br/>'
										. 'Work Phone: ' . $res_sub->work_phone;
				
			}
		}
		
	
		// Load the "return loan" view
		$this->data['main_content'] = $this->load->view('staff/loan_management/return_loan', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
	
	public function extendLoan( $access_id = 0 )
	{
		// If 'POST' data exists (i.e. the submit button was clicked)
		if ( $this->input->post('extendLoan') )
		{
			// If form validation succeeds (All fields correct)
			if ($this->form_validation->run('staff/loan_management/extendLoan'))
			{
				$data = array();
				$data['extension_date'] = $this->input->post('extension_date');
				$data['due_date'] = $this->input->post('due_date');
				$this->loan_model->updateLoan($access_id, $data);
                            
				$this->data['error'] = 'Item successfully extended.';
			}
		}
		
		// If no access_id has been provided by url or post...
		if ( !$access_id && !$this->input->post('access_id') )
		{
			// Load the "extend_loan_select_item" view
			$this->data['main_content'] = $this->load->view('staff/loan_management/extend_loan_select_item', $this->data, true);
		
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);	
		}
		
		else
		{
			if (!$access_id)
				$access_id = $this->input->post('access_id');
			
			// Return loan information
			$this->data['loan'] = $this->loan_model->getLoanByID( $access_id )->row();

			// Return subscriber information
			$this->data['subscriber'] = $this->subscriber_model->getSubscriberByID($this->data['loan']->subscriber_id)->row();

			// Return loan information for specified subscriber
			$this->data['volume'] = $this->volume_model->getVolumeByID( $this->data['loan']->access_id )->row();
			
			// Load the "extend_loan_select_item" view
			$this->data['main_content'] = $this->load->view('staff/loan_management/extend_loan', $this->data, true);
		
			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}
	}
	
	public function reserveLoan( $access_id = 0, $subscriber_id = 0 )
	{
		// If no access_id specified in first argument, get one
		if (!$access_id)
		{
		    $this->data['main_content'] = $this->load->view('staff/loan_management/reserve_loan_select_item', $this->data, true);

		    // Load $this->data['main_content'] in the main template
		    $this->load->view($this->template, $this->data);			    
		}
		
		// Otherwise, display page allowing for the user to select a subscriber to reserve the item for or remove a reservation
		else 
		{
			// If the user chose a reserve subscriber
			if ($subscriber_id && $access_id)
			{
				$data = array();
				if ($subscriber_id == 'none')
					$data['reserve_subscriber'] = '0';
					
				else
					$data['reserve_subscriber'] = $subscriber_id;
				$this->loan_model->updateLoan($access_id, $data);
				$this->data['error'] = 'Item #' . $access_id . ' successfully reserved for subscriber #' . $subscriber_id;
			}
			
			// Return the volume that has the specified access_id and then add the volume_table to view
			$this->data['volume'] = $this->volume_model->getVolumeByID( $access_id )->row();

			// Return loan information for specified volume
			$this->data['loan'] = $this->loan_model->getLoanByID( $access_id )->row();
			
			// Return the subscriber's name
			$this->data['subscriber'] = $this->subscriber_model->getSubscriberByID( $this->data['loan']->subscriber_id )->row();
			
			// Return the reserved subscriber's name
			$this->data['reserve_subscriber'] = $this->subscriber_model->getSubscriberByID($this->data['loan']->reserve_subscriber)->row();

			$this->data['main_content'] = $this->load->view('staff/loan_management/reserve_loan', $this->data, true);

			// Load $this->data['main_content'] in the main template
			$this->load->view($this->template, $this->data);
		}
	}
}

?>